#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaToggleButton.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QDebug>

class ElaToggleButtonTest : public ElaWidget
{
    Q_OBJECT

  public:
    ElaToggleButtonTest(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaToggleButton 功能演示");
        resize(700, 600);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        setupUI();
        setupConnections();
    }

  private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 基础切换按钮展示
        mainLayout->addWidget(createBasicToggleGroup());

        // 2. 圆角样式展示
        mainLayout->addWidget(createBorderRadiusGroup());

        // 3. 状态演示
        mainLayout->addWidget(createStateDemoGroup());

        // 4. 尺寸和文本演示
        mainLayout->addWidget(createSizeTextGroup());

        // 5. 实际应用场景
        mainLayout->addWidget(createApplicationGroup());

        // 6. 动态操作演示
        mainLayout->addWidget(createDynamicOperationGroup());

        mainLayout->addStretch();
    }

    // 创建基础切换按钮组
    QGroupBox *createBasicToggleGroup()
    {
        QGroupBox *group = new QGroupBox("基础切换按钮", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示基本的切换按钮功能", this);
        layout->addWidget(infoLabel);

        QHBoxLayout *buttonLayout = new QHBoxLayout();

        // 默认状态按钮
        ElaToggleButton *defaultBtn = new ElaToggleButton("默认关闭", this);

        // 默认开启状态按钮
        ElaToggleButton *enabledBtn = new ElaToggleButton("默认开启", this);
        enabledBtn->setIsToggled(true);

        // 不同文本按钮
        ElaToggleButton *textBtn1 = new ElaToggleButton("开关", this);
        ElaToggleButton *textBtn2 = new ElaToggleButton("启用", this);

        buttonLayout->addWidget(defaultBtn);
        buttonLayout->addWidget(enabledBtn);
        buttonLayout->addWidget(textBtn1);
        buttonLayout->addWidget(textBtn2);
        buttonLayout->addStretch();

        layout->addLayout(buttonLayout);

        m_basicToggles << defaultBtn << enabledBtn << textBtn1 << textBtn2;

        return group;
    }

    // 创建圆角样式展示组
    QGroupBox *createBorderRadiusGroup()
    {
        QGroupBox *group = new QGroupBox("圆角样式展示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同圆角大小的切换按钮", this);
        layout->addWidget(infoLabel);

        QHBoxLayout *buttonLayout = new QHBoxLayout();

        // 不同圆角大小的按钮
        ElaToggleButton *sharpBtn = new ElaToggleButton("直角(0)", this);
        sharpBtn->setBorderRadius(0);

        ElaToggleButton *smallRoundBtn = new ElaToggleButton("小圆角(2)", this);
        smallRoundBtn->setBorderRadius(2);

        ElaToggleButton *defaultBtn = new ElaToggleButton("默认(3)", this);
        defaultBtn->setBorderRadius(3);

        ElaToggleButton *mediumRoundBtn = new ElaToggleButton("中圆角(6)", this);
        mediumRoundBtn->setBorderRadius(6);

        ElaToggleButton *largeRoundBtn = new ElaToggleButton("大圆角(12)", this);
        largeRoundBtn->setBorderRadius(12);

        buttonLayout->addWidget(sharpBtn);
        buttonLayout->addWidget(smallRoundBtn);
        buttonLayout->addWidget(defaultBtn);
        buttonLayout->addWidget(mediumRoundBtn);
        buttonLayout->addWidget(largeRoundBtn);
        buttonLayout->addStretch();

        layout->addLayout(buttonLayout);

        m_radiusToggles << sharpBtn << smallRoundBtn << defaultBtn << mediumRoundBtn << largeRoundBtn;

        return group;
    }

    // 创建状态演示组
    QGroupBox *createStateDemoGroup()
    {
        QGroupBox *group = new QGroupBox("状态演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示切换按钮的不同状态", this);
        layout->addWidget(infoLabel);

        // 正常状态
        QHBoxLayout *normalLayout = new QHBoxLayout();
        QLabel *normalLabel = new QLabel("正常状态:", this);
        ElaToggleButton *normalOff = new ElaToggleButton("关闭", this);
        ElaToggleButton *normalOn = new ElaToggleButton("开启", this);
        normalOn->setIsToggled(true);

        normalLayout->addWidget(normalLabel);
        normalLayout->addWidget(normalOff);
        normalLayout->addWidget(normalOn);
        normalLayout->addStretch();

        // 禁用状态
        QHBoxLayout *disabledLayout = new QHBoxLayout();
        QLabel *disabledLabel = new QLabel("禁用状态:", this);
        ElaToggleButton *disabledOff = new ElaToggleButton("禁用关", this);
        disabledOff->setEnabled(false);
        ElaToggleButton *disabledOn = new ElaToggleButton("禁用开", this);
        disabledOn->setIsToggled(true);
        disabledOn->setEnabled(false);

        disabledLayout->addWidget(disabledLabel);
        disabledLayout->addWidget(disabledOff);
        disabledLayout->addWidget(disabledOn);
        disabledLayout->addStretch();

        layout->addLayout(normalLayout);
        layout->addLayout(disabledLayout);

        m_stateToggles << normalOff << normalOn << disabledOff << disabledOn;

        return group;
    }

    // 创建尺寸和文本演示组
    QGroupBox *createSizeTextGroup()
    {
        QGroupBox *group = new QGroupBox("尺寸和文本演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同尺寸和文本内容的切换按钮", this);
        layout->addWidget(infoLabel);

        // 不同尺寸
        QHBoxLayout *sizeLayout = new QHBoxLayout();
        QLabel *sizeLabel = new QLabel("尺寸:", this);

        ElaToggleButton *smallBtn = new ElaToggleButton("小", this);
        smallBtn->setFixedSize(60, 24);

        ElaToggleButton *mediumBtn = new ElaToggleButton("中", this);
        mediumBtn->setFixedSize(80, 32);  // 默认尺寸

        ElaToggleButton *largeBtn = new ElaToggleButton("大", this);
        largeBtn->setFixedSize(100, 40);

        sizeLayout->addWidget(sizeLabel);
        sizeLayout->addWidget(smallBtn);
        sizeLayout->addWidget(mediumBtn);
        sizeLayout->addWidget(largeBtn);
        sizeLayout->addStretch();

        // 不同文本长度
        QHBoxLayout *textLayout = new QHBoxLayout();
        QLabel *textLabel = new QLabel("文本:", this);

        ElaToggleButton *shortTextBtn = new ElaToggleButton("开", this);
        ElaToggleButton *mediumTextBtn = new ElaToggleButton("开关", this);
        ElaToggleButton *longTextBtn = new ElaToggleButton("启用功能", this);
        longTextBtn->setFixedSize(120, 32);

        textLayout->addWidget(textLabel);
        textLayout->addWidget(shortTextBtn);
        textLayout->addWidget(mediumTextBtn);
        textLayout->addWidget(longTextBtn);
        textLayout->addStretch();

        layout->addLayout(sizeLayout);
        layout->addLayout(textLayout);

        m_sizeTextToggles << smallBtn << mediumBtn << largeBtn << shortTextBtn << mediumTextBtn << longTextBtn;

        return group;
    }

    // 创建实际应用场景组
    QGroupBox *createApplicationGroup()
    {
        QGroupBox *group = new QGroupBox("实际应用场景", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示切换按钮在实际场景中的应用", this);
        layout->addWidget(infoLabel);

        // 设置选项
        QGroupBox *settingsGroup = new QGroupBox("应用程序设置", this);
        QVBoxLayout *settingsLayout = new QVBoxLayout(settingsGroup);

        QHBoxLayout *autoSaveLayout = new QHBoxLayout();
        QLabel *autoSaveLabel = new QLabel("自动保存:", this);
        m_autoSaveToggle = new ElaToggleButton("启用", this);
        m_autoSaveToggle->setIsToggled(true);
        autoSaveLayout->addWidget(autoSaveLabel);
        autoSaveLayout->addWidget(m_autoSaveToggle);
        autoSaveLayout->addStretch();

        QHBoxLayout *notificationsLayout = new QHBoxLayout();
        QLabel *notificationsLabel = new QLabel("桌面通知:", this);
        m_notificationsToggle = new ElaToggleButton("启用", this);
        notificationsLayout->addWidget(notificationsLabel);
        notificationsLayout->addWidget(m_notificationsToggle);
        notificationsLayout->addStretch();

        QHBoxLayout *autoUpdateLayout = new QHBoxLayout();
        QLabel *autoUpdateLabel = new QLabel("自动更新:", this);
        m_autoUpdateToggle = new ElaToggleButton("启用", this);
        autoUpdateLayout->addWidget(autoUpdateLabel);
        autoUpdateLayout->addWidget(m_autoUpdateToggle);
        autoUpdateLayout->addStretch();

        settingsLayout->addLayout(autoSaveLayout);
        settingsLayout->addLayout(notificationsLayout);
        settingsLayout->addLayout(autoUpdateLayout);

        // 功能开关
        QGroupBox *featuresGroup = new QGroupBox("功能开关", this);
        QVBoxLayout *featuresLayout = new QVBoxLayout(featuresGroup);

        QHBoxLayout *darkModeLayout = new QHBoxLayout();
        QLabel *darkModeLabel = new QLabel("暗色模式:", this);
        m_darkModeToggle = new ElaToggleButton("开启", this);
        darkModeLayout->addWidget(darkModeLabel);
        darkModeLayout->addWidget(m_darkModeToggle);
        darkModeLayout->addStretch();

        QHBoxLayout *syncLayout = new QHBoxLayout();
        QLabel *syncLabel = new QLabel("云同步:", this);
        m_syncToggle = new ElaToggleButton("启用", this);
        syncLayout->addWidget(syncLabel);
        syncLayout->addWidget(m_syncToggle);
        syncLayout->addStretch();

        featuresLayout->addLayout(darkModeLayout);
        featuresLayout->addLayout(syncLayout);

        layout->addWidget(settingsGroup);
        layout->addWidget(featuresGroup);

        m_applicationToggles << m_autoSaveToggle << m_notificationsToggle << m_autoUpdateToggle << m_darkModeToggle << m_syncToggle;

        return group;
    }

    // 创建动态操作演示组
    QGroupBox *createDynamicOperationGroup()
    {
        QGroupBox *group = new QGroupBox("动态操作演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("演示切换按钮的动态操作", this);
        layout->addWidget(infoLabel);

        // 动态修改文本
        QHBoxLayout *dynamicTextLayout = new QHBoxLayout();
        m_dynamicTextToggle = new ElaToggleButton("原始文本", this);
        QPushButton *changeTextBtn = new QPushButton("修改文本", this);

        dynamicTextLayout->addWidget(m_dynamicTextToggle);
        dynamicTextLayout->addWidget(changeTextBtn);
        dynamicTextLayout->addStretch();

        // 动态启用/禁用
        QHBoxLayout *enableDisableLayout = new QHBoxLayout();
        m_toggleEnableToggle = new ElaToggleButton("启用/禁用测试", this);
        m_toggleEnableToggle->setIsToggled(true);
        QPushButton *toggleEnableBtn = new QPushButton("切换启用状态", this);

        enableDisableLayout->addWidget(m_toggleEnableToggle);
        enableDisableLayout->addWidget(toggleEnableBtn);
        enableDisableLayout->addStretch();

        // 批量操作
        QHBoxLayout *batchLayout = new QHBoxLayout();
        QLabel *batchLabel = new QLabel("批量操作:", this);
        QPushButton *toggleAllOnBtn = new QPushButton("全部开启", this);
        QPushButton *toggleAllOffBtn = new QPushButton("全部关闭", this);
        QPushButton *invertAllBtn = new QPushButton("全部反转", this);

        batchLayout->addWidget(batchLabel);
        batchLayout->addWidget(toggleAllOnBtn);
        batchLayout->addWidget(toggleAllOffBtn);
        batchLayout->addWidget(invertAllBtn);
        batchLayout->addStretch();

        // 批量操作的目标按钮
        QHBoxLayout *targetLayout = new QHBoxLayout();
        m_batchToggle1 = new ElaToggleButton("选项A", this);
        m_batchToggle2 = new ElaToggleButton("选项B", this);
        m_batchToggle3 = new ElaToggleButton("选项C", this);
        m_batchToggle4 = new ElaToggleButton("选项D", this);

        targetLayout->addWidget(m_batchToggle1);
        targetLayout->addWidget(m_batchToggle2);
        targetLayout->addWidget(m_batchToggle3);
        targetLayout->addWidget(m_batchToggle4);
        targetLayout->addStretch();

        // 状态显示
        QHBoxLayout *statusLayout = new QHBoxLayout();
        m_statusDisplay = new QLabel("开启状态: 0/4", this);
        updateStatusDisplay();

        statusLayout->addWidget(m_statusDisplay);
        statusLayout->addStretch();

        layout->addLayout(dynamicTextLayout);
        layout->addLayout(enableDisableLayout);
        layout->addLayout(batchLayout);
        layout->addLayout(targetLayout);
        layout->addLayout(statusLayout);

        m_dynamicToggles << m_dynamicTextToggle << m_toggleEnableToggle << m_batchToggle1 << m_batchToggle2 << m_batchToggle3 << m_batchToggle4;
        m_operationButtons << changeTextBtn << toggleEnableBtn << toggleAllOnBtn << toggleAllOffBtn << invertAllBtn;

        return group;
    }

    void setupConnections()
    {
        // 动态文本修改
        connect(m_operationButtons[0], &QPushButton::clicked, this, [this]() {
            static int clickCount = 0;
            clickCount++;
            QString newText = QString("动态文本%1").arg(clickCount);
            m_dynamicTextToggle->setText(newText);
            qDebug() << "切换按钮文本已修改为:" << newText;
        });

        // 启用/禁用切换
        connect(m_operationButtons[1], &QPushButton::clicked, this, [this]() {
            bool currentState = m_toggleEnableToggle->isEnabled();
            m_toggleEnableToggle->setEnabled(!currentState);
            QString stateText = !currentState ? "已启用" : "已禁用";
            qDebug() << "切换按钮状态:" << stateText;
        });

        // 批量操作 - 全部开启
        connect(m_operationButtons[2], &QPushButton::clicked, this, [this]() {
            m_batchToggle1->setIsToggled(true);
            m_batchToggle2->setIsToggled(true);
            m_batchToggle3->setIsToggled(true);
            m_batchToggle4->setIsToggled(true);
            updateStatusDisplay();
            qDebug() << "已开启所有选项";
        });

        // 批量操作 - 全部关闭
        connect(m_operationButtons[3], &QPushButton::clicked, this, [this]() {
            m_batchToggle1->setIsToggled(false);
            m_batchToggle2->setIsToggled(false);
            m_batchToggle3->setIsToggled(false);
            m_batchToggle4->setIsToggled(false);
            updateStatusDisplay();
            qDebug() << "已关闭所有选项";
        });

        // 批量操作 - 全部反转
        connect(m_operationButtons[4], &QPushButton::clicked, this, [this]() {
            m_batchToggle1->setIsToggled(!m_batchToggle1->getIsToggled());
            m_batchToggle2->setIsToggled(!m_batchToggle2->getIsToggled());
            m_batchToggle3->setIsToggled(!m_batchToggle3->getIsToggled());
            m_batchToggle4->setIsToggled(!m_batchToggle4->getIsToggled());
            updateStatusDisplay();
            qDebug() << "已反转所有选项状态";
        });

        // 批量操作按钮状态变化
        connect(m_batchToggle1, &ElaToggleButton::toggled, this, &ElaToggleButtonTest::updateStatusDisplay);
        connect(m_batchToggle2, &ElaToggleButton::toggled, this, &ElaToggleButtonTest::updateStatusDisplay);
        connect(m_batchToggle3, &ElaToggleButton::toggled, this, &ElaToggleButtonTest::updateStatusDisplay);
        connect(m_batchToggle4, &ElaToggleButton::toggled, this, &ElaToggleButtonTest::updateStatusDisplay);

        // 应用设置信号
        connect(m_autoSaveToggle, &ElaToggleButton::toggled, this, [this](bool checked) { qDebug() << "自动保存:" << (checked ? "启用" : "禁用"); });

        connect(m_darkModeToggle, &ElaToggleButton::toggled, this, [this](bool checked) {
            qDebug() << "暗色模式:" << (checked ? "开启" : "关闭");
            // 在实际应用中，这里可以触发主题切换
            // eTheme->setThemeMode(checked ? ElaThemeType::Dark : ElaThemeType::Light);
        });

        // 基础按钮信号
        for (ElaToggleButton *toggle : m_basicToggles + m_radiusToggles + m_stateToggles + m_sizeTextToggles)
        {
            connect(toggle, &ElaToggleButton::toggled, this, [toggle](bool checked) { qDebug() << "切换按钮状态变更:" << toggle->getText() << "=" << (checked ? "开启" : "关闭"); });
        }
    }

  private Q_SLOTS:
    void updateStatusDisplay()
    {
        int enabledCount = 0;
        if (m_batchToggle1->getIsToggled())
            enabledCount++;
        if (m_batchToggle2->getIsToggled())
            enabledCount++;
        if (m_batchToggle3->getIsToggled())
            enabledCount++;
        if (m_batchToggle4->getIsToggled())
            enabledCount++;

        QString statusText = QString("开启状态: %1/4").arg(enabledCount);
        m_statusDisplay->setText(statusText);
    }

  private:
    // 基础切换按钮
    QList<ElaToggleButton *> m_basicToggles;

    // 圆角演示
    QList<ElaToggleButton *> m_radiusToggles;

    // 状态演示
    QList<ElaToggleButton *> m_stateToggles;

    // 尺寸文本演示
    QList<ElaToggleButton *> m_sizeTextToggles;

    // 应用演示
    QList<ElaToggleButton *> m_applicationToggles;

    // 动态操作
    QList<ElaToggleButton *> m_dynamicToggles;
    QList<QPushButton *> m_operationButtons;

    // 动态操作控件指针
    ElaToggleButton *m_dynamicTextToggle;
    ElaToggleButton *m_toggleEnableToggle;
    ElaToggleButton *m_batchToggle1;
    ElaToggleButton *m_batchToggle2;
    ElaToggleButton *m_batchToggle3;
    ElaToggleButton *m_batchToggle4;

    // 应用场景控件指针
    ElaToggleButton *m_autoSaveToggle;
    ElaToggleButton *m_notificationsToggle;
    ElaToggleButton *m_autoUpdateToggle;
    ElaToggleButton *m_darkModeToggle;
    ElaToggleButton *m_syncToggle;

    QLabel *m_statusDisplay;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaToggleButtonTest demo;
    demo.moveToCenter();
    demo.show();

    return app.exec();
}

#include "ElaToggleButtonTest.moc"